package vista;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JButton;

import modelo.Jugador;
import modelo.Tablero;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PantallaInicial {

	private JFrame frame;
	private JTextField txtNombre;
	private JTextField textFieldNombre;
	private JTextField txtIngreseMetas;
	private JTextField txtEducacion;
	private JTextField txtFelicidad;
	private JTextField txtRiqueza;
	private JTextField txtCarreraLaboral;
	private JSpinner spinnerCarreraLaboral;
	private JSpinner spinnerEducacion;
	private JSpinner spinnerFelicidad;
	private JSpinner spinnerRiqueza;
	private static Jugador jugador;
	private static Tablero tablero;
	private JTextField txtIngreseUn;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PantallaInicial window = new PantallaInicial();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public PantallaInicial() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(Color.LIGHT_GRAY);
		frame.setBounds(100, 100, 320, 365);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		txtNombre = new JTextField();
		txtNombre.setEditable(false);
		txtNombre.setBorder(null);
		txtNombre.setBackground(Color.LIGHT_GRAY);
		txtNombre.setText("Nombre");
		txtNombre.setColumns(10);
		
		textFieldNombre = new JTextField();
		textFieldNombre.setBorder(null);
		textFieldNombre.setColumns(10);
		
		txtIngreseMetas = new JTextField();
		txtIngreseMetas.setEditable(false);
		txtIngreseMetas.setText("Ingrese metas");
		txtIngreseMetas.setColumns(10);
		txtIngreseMetas.setBorder(null);
		txtIngreseMetas.setBackground(Color.LIGHT_GRAY);
		
		txtEducacion = new JTextField();
		txtEducacion.setEditable(false);
		txtEducacion.setText("Educacion");
		txtEducacion.setColumns(10);
		txtEducacion.setBorder(null);
		txtEducacion.setBackground(Color.LIGHT_GRAY);
		
		txtFelicidad = new JTextField();
		txtFelicidad.setEditable(false);
		txtFelicidad.setText("Felicidad");
		txtFelicidad.setColumns(10);
		txtFelicidad.setBorder(null);
		txtFelicidad.setBackground(Color.LIGHT_GRAY);
		
		txtRiqueza = new JTextField();
		txtRiqueza.setEditable(false);
		txtRiqueza.setText("Riqueza");
		txtRiqueza.setColumns(10);
		txtRiqueza.setBorder(null);
		txtRiqueza.setBackground(Color.LIGHT_GRAY);
		
		txtCarreraLaboral = new JTextField();
		txtCarreraLaboral.setEditable(false);
		txtCarreraLaboral.setText("Carrera Laboral");
		txtCarreraLaboral.setColumns(10);
		txtCarreraLaboral.setBorder(null);
		txtCarreraLaboral.setBackground(Color.LIGHT_GRAY);
		
		jugador = Jugador.getInstacia();
		tablero = Tablero.getInstacia();
		
		spinnerEducacion = new JSpinner();
		spinnerEducacion.setBackground(Color.WHITE);
		spinnerEducacion.setModel(new SpinnerNumberModel(new Integer(2000), null, null, new Integer(1)));
		
		spinnerFelicidad = new JSpinner();
		spinnerFelicidad.setBackground(Color.WHITE);
		spinnerFelicidad.setModel(new SpinnerNumberModel(new Integer(2000), null, null, new Integer(1)));
		
		spinnerRiqueza = new JSpinner();
		spinnerRiqueza.setBackground(Color.WHITE);
		spinnerRiqueza.setModel(new SpinnerNumberModel(new Integer(2000), null, null, new Integer(1)));
		
		spinnerCarreraLaboral = new JSpinner();
		spinnerCarreraLaboral.setBackground(Color.WHITE);
		spinnerCarreraLaboral.setModel(new SpinnerNumberModel(new Integer(2000), null, null, new Integer(1)));
		
		txtIngreseUn = new JTextField();
		txtIngreseUn.setVisible(false);
		txtIngreseUn.setForeground(Color.RED);
		txtIngreseUn.setEditable(false);
		txtIngreseUn.setBackground(Color.LIGHT_GRAY);
		txtIngreseUn.setText("* Ingrese un nombre");
		txtIngreseUn.setBorder(null);
		txtIngreseUn.setColumns(10);
		
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				if(textFieldNombre.getText().length() == 0){
					txtIngreseUn.setVisible(true);
					frame.setBounds(100, 100, 400, 365);
				}
				else{
					int n = JOptionPane.showConfirmDialog(
					    frame,
					    "Esta seguro que desea aceptar?",
					    "Aceptar",
					    JOptionPane.YES_NO_OPTION);
					if(n == JOptionPane.YES_OPTION){
						jugador.setNombre(textFieldNombre.getText());
						jugador.getMeta().setEducacion(Integer.parseInt(spinnerEducacion.getValue().toString()));
						jugador.getMeta().setFelicidad(Integer.parseInt(spinnerFelicidad.getValue().toString()));
						jugador.getMeta().setRiqueza(Integer.parseInt(spinnerRiqueza.getValue().toString()));
						jugador.getMeta().setCarreraLaboral(Integer.parseInt(spinnerCarreraLaboral.getValue().toString()));
						PantallaPrincipal pantallaPricipal =new PantallaPrincipal();
						pantallaPricipal.main(null);
						frame.dispose();
					}
				}
				
			}
		});
		
		JButton btnLimpiar = new JButton("Limpiar");
		btnLimpiar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textFieldNombre.setText("");
				spinnerCarreraLaboral.setValue(2000);
				spinnerEducacion.setValue(2000);
				spinnerFelicidad.setValue(2000);
				spinnerRiqueza.setValue(2000);
			}
		});
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n = JOptionPane.showConfirmDialog(
					    frame,
					    "Esta seguro que desea cancelar",
					    "Cancelar",
					    JOptionPane.YES_NO_OPTION);
				if(n == JOptionPane.YES_OPTION){
					frame.dispose();
				}
			}
		});
		
		
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(txtIngreseMetas, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnAceptar)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnLimpiar, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnCancelar))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(txtCarreraLaboral, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(spinnerCarreraLaboral, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(txtRiqueza, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(spinnerRiqueza, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(txtFelicidad, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(spinnerFelicidad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(txtEducacion, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(spinnerEducacion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(txtNombre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textFieldNombre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txtIngreseUn, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(37, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(33)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtNombre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldNombre, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtIngreseUn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(txtIngreseMetas, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtEducacion, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinnerEducacion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtFelicidad, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinnerFelicidad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtRiqueza, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinnerRiqueza, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtCarreraLaboral, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinnerCarreraLaboral, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnAceptar)
						.addComponent(btnLimpiar)
						.addComponent(btnCancelar))
					.addContainerGap())
		);
		frame.getContentPane().setLayout(groupLayout);
	}
	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}
}
